package com.matt.ext.aliyun.impl;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CopyObjectRequest;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.matt.ext.aliyun.OSSProcessService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.io.File;
import java.io.IOException;

/**
 * Created with teach-system
 * User : Ting.Yao
 * Date : 2014/12/24.
 */
@Service
@Transactional
public class OSSProcessServiceImpl implements OSSProcessService {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Override
    public String aliyunOssUpload(String relativePath, MultipartFile file) {
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            if (!ossClient.doesBucketExist(bucketName)) {
                /*
                 * Create a new OSS bucket
                 */
                ossClient.createBucket(bucketName);
                CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                ossClient.createBucket(createBucketRequest);
            }

            ossClient.putObject(new PutObjectRequest(bucketName, relativePath, file.getInputStream()));
            ossClient.setObjectAcl(bucketName, relativePath, CannedAccessControlList.PublicRead);
            ossClient.setObjectAcl(bucketName, relativePath, CannedAccessControlList.Default);

        } catch (OSSException oe) {

        } catch (ClientException ce) {

        } catch (IOException ie){

        } finally{
            ossClient.shutdown();
        }
        return relativePath;
    }

    @Override
    public String aliyunOssUpload(String relativePath, File file) {
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            if (!ossClient.doesBucketExist(bucketName)) {
                /*
                 * Create a new OSS bucket
                 */
                ossClient.createBucket(bucketName);
                CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                ossClient.createBucket(createBucketRequest);
            }

            ossClient.putObject(new PutObjectRequest(bucketName, relativePath, file));
            ossClient.setObjectAcl(bucketName, relativePath, CannedAccessControlList.PublicRead);
            ossClient.setObjectAcl(bucketName, relativePath, CannedAccessControlList.Default);

        } catch (OSSException oe) {

        } catch (ClientException ce) {

        }finally{
            ossClient.shutdown();
        }
        return relativePath;
    }

    @Override
    public void aliyunOssDelete(String relativePath) {
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        ossClient.deleteObject(bucketName, relativePath);
    }

    @Override
    public void aliyunOssCopy() {
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest("", "", "", "");
    }
}
